package com.hurl.waguji.biz.download;

import java.util.Date;

import org.apache.log4j.Logger;

import com.hurl.library.spider.SpiderException;
import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.DateUtils;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.DateService;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjCjmxHexunBO;
import com.hurl.waguji.persistence.JcsjCjmxHexunBOId;

class MrhqCjmxHexunProcessor extends BaseDownloadProcessor{
	private static final Logger log = Logger.getLogger(MrhqCjmxHexunProcessor.class);

	public MrhqCjmxHexunProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
		context.setBoSaveMode(DownloadContext.BO_SAVE_MODE_INSERT);
	}

	private Date lastTradingTime;
	private Date lastTradingDate;
	private String dateStr;
	private Date date = DateService.getCurrentTimeFromServer();


	@Override
	public void downloadFailure(String url, Throwable e) throws SpiderException {
		
	}

	@Override
	public Date getRq() {
		return lastTradingDate;
	}

	@Override
	public void validate() throws Throwable {
		lastTradingTime = DateService.getCurrentTimeFromWeb();
		lastTradingDate = DateUtils.getDate(lastTradingTime);
		dateStr = DateUtils.formatDate(lastTradingDate, "yyyy-MM-dd");
		
		this.validateNowAfterTradingTime(lastTradingTime);
	}

	@Override
	public boolean beforeDownload(String url) throws SpiderException {
		/* 下载过的项就不要重复下载了 */
		if(context.getDao().queryBySQL(SqlReader.getSql("SQL12041404",new String[]{context.getEnvAsString("gpdm"),dateStr})).setReadOnly(true).list().size()>0){
			log.debug(context.getEnvAsString("gpdm")+"已经下载过！");
			return false;
		}
		return true;
	}

	private void convertHTML(String html,JcsjCjmxHexunBO bo){
		
		String value = StringUtils.substringBetween(html, "股票简称</td>", "</td>");
		value = StringUtils.substringBetween(value,"<td align=\"right\">",null);
		bo.setGpmc(Formatter.toString(value));
		
		value = StringUtils.substringBetween(html, "买入成交(笔)</td>", "</td>");
		value = StringUtils.substringBetween(value,"<td align=\"right\">",null);
		bo.setMrcjbs(Formatter.toInteger(value));
		
		value = StringUtils.substringBetween(html, "卖出成交(笔)</td>", "</td>");
		value = StringUtils.substringBetween(value,"<td align=\"right\">",null);
		bo.setMccjbs(Formatter.toInteger(value));
		
		value = StringUtils.substringBetween(html, "买入成交金额(万元)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setMrcjje(Formatter.toDouble(value)*10000);
		
		value = StringUtils.substringBetween(html, "卖出成交金额(万元)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setMccjje(Formatter.toDouble(value)*10000);
		
		value = StringUtils.substringBetween(html, "成交笔数(笔数)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setCjbs(Formatter.toInteger(value));
		
		value = StringUtils.substringBetween(html, "买入成交均价(元)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setMrcjjj(Formatter.toDouble(value));
		
		value = StringUtils.substringBetween(html, "卖出成交均价(元)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setMccjjj(Formatter.toDouble(value));
		
		value = StringUtils.substringBetween(html, "成交金额(万元)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setCjje(Formatter.toDouble(value)*10000);
		
		value = StringUtils.substringBetween(html, "买入平均每笔成交金额(万元)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setMrpjdbcjje(Formatter.toDouble(value)*10000);
		
		value = StringUtils.substringBetween(html, "卖出平均每笔成交金额(万元)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setMcpjdbcjje(Formatter.toDouble(value)*10000);

		value = StringUtils.substringBetween(html, "成交均价(元)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setCjjj(Formatter.toDouble(value));
		
		value = StringUtils.substringBetween(html, "买入占总成交金额(%)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setMrzcjje(Formatter.toDouble(value)/100);
		
		value = StringUtils.substringBetween(html, "卖出占总成交金额(%)</td>", "</td>");
		value = StringUtils.substringBetween(value,">",null);
		bo.setMczcjje(Formatter.toDouble(value)/100);
		
		bo.setLastupdate(date);
		
	}
	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return ;
		}
		String gpdm = StringUtils.substringBetween(url, "gpdm=", null);
		JcsjCjmxHexunBO bo = null;
		
		bo = new JcsjCjmxHexunBO();
		String sh = StringUtils.substringBetween(html, "散单成交统计", "</table>");
		bo.setId(new JcsjCjmxHexunBOId(gpdm,lastTradingDate,1));
		this.convertHTML(sh, bo);
		collector.addBO(bo);
		
		bo = new JcsjCjmxHexunBO();
		sh = StringUtils.substringBetween(html, "小单成交统计", "</table>");
		bo.setId(new JcsjCjmxHexunBOId(gpdm,lastTradingDate,2));
		this.convertHTML(sh, bo);
		collector.addBO(bo);
		
		bo = new JcsjCjmxHexunBO();
		sh = StringUtils.substringBetween(html, "大单成交分布", "</table>");
		bo.setId(new JcsjCjmxHexunBOId(gpdm,lastTradingDate,3));
		this.convertHTML(sh, bo);
		collector.addBO(bo);
		
		bo = new JcsjCjmxHexunBO();
		sh = StringUtils.substringBetween(html, "超级大单成交分布", "</table>");
		bo.setId(new JcsjCjmxHexunBOId(gpdm,lastTradingDate,4));
		this.convertHTML(sh, bo);
		collector.addBO(bo);
	}

	@Override
	public void shutdown(boolean success, Throwable t) {
		if(!success){
//			context.getDao().queryByHQL("delete from "+JcsjCjmxHexunBO.class.getSimpleName()+" where id.rq=:rq").setDate("rq", lastTradingDate).executeUpdate();
		}
	}

	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://vol.stock.hexun.com/Close/share/Detail.aspx?rank=3&code="+bo.getGpDmJ()+"&gpdm="+bo.getGpDm();
			}
		};
	}
}
